package com.graduate.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.graduate.mapper.AcctTypeMapper;
import com.graduate.mapper.DepartmentMapper;
import com.graduate.mapper.FundJournalMapper;
import com.graduate.mapper.RegressionLineMapper;
import com.graduate.model.AcctType;
import com.graduate.model.CountExportToExcel;
import com.graduate.model.Department;
import com.graduate.model.FundJournal;
import com.graduate.model.FundJournalExport;
import com.graduate.model.PercentExportToExcel;
import com.graduate.model.RegressionLine;
import com.graduate.service.IFundJournalService;
import com.graduate.util.DataPoint;
import com.graduate.util.PageInfo;
import com.graduate.util.RegressionLineUtils;
import com.graduate.util.ResponseResult;
@Service("fundJournalService")
public class FundJournalServiceImpl implements IFundJournalService {

	@Autowired
	public FundJournalMapper fundJournalMapper;
	@Autowired
	public AcctTypeMapper acctTypeMapper;
	@Autowired
	public DepartmentMapper departmentMapper;
	@Autowired
	public RegressionLineMapper regressionLineMapper;
	
	@Override
	public int insertFundJournal(FundJournal fundJournal) {
		 int row=fundJournalMapper.insert(fundJournal);
		return row;
	}
	@Override
	public ResponseResult updateByPrimaryKey(FundJournal fundJournal){
		int row=fundJournalMapper.updateByPrimaryKey(fundJournal);
		if(row>0){
			return ResponseResult.createBySuccessCode();
		}else{
			return ResponseResult.createByFailedCode();
		}
	}
	@Override
	public ResponseResult deleteByPrimaryKey(String id){
		int row=fundJournalMapper.deleteByPrimaryKey(id);
		if(row>0){
			return ResponseResult.createBySuccessCode();
		}else{
			return ResponseResult.createByFailedCode();
		}
	}
	
	@Override
	public List<AcctType> listAcctType(Map<String,Object> map) {
		
		return 	acctTypeMapper.selectAll(map);
	
	}
	
	@Override
	public	List<Department> listSource(){
		return departmentMapper.selectAll();
	}
	
	
	@Override
	public PageInfo listDataState(PageInfo pageInfo){
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getPagesize());
		List<Map<String, Object>> list =  fundJournalMapper.listDataState(page,pageInfo.getCondition());
		pageInfo.setRows(list);
		pageInfo.setTotal(page.getTotal());
		return pageInfo;
		
	}
	
	@Override
	public List<FundJournalExport> listDataStateForExcel(Map<String, Object> condition){
		List<FundJournalExport> list =  fundJournalMapper.listDataStateForExcel(condition);
		return list;
	}
	
	@Override
	public List<Map<String, Object>> highchartsShow(Map<String, Object> condition){
		return fundJournalMapper.highchartsShow(condition);
	}
	@Override
	public List<Map<String, Object>> highchartsShow1(Map<String, Object> condition){
		return fundJournalMapper.highchartsShow1(condition);
	}
	@Override
	public List<Map<String, Object>> highchartsShowWeek(Map<String, Object> condition){
		return fundJournalMapper.highchartsShowWeek(condition);
	}
	@Override
	public List<Map<String, Object>> highchartsShowMonth(Map<String, Object> condition){
		return fundJournalMapper.highchartsShowMonth(condition);
	}
	@Override
	public List<Map<String, Object>> highchartsShowLastMonth(Map<String, Object> condition){
		return fundJournalMapper.highchartsShowLastMonth(condition);
	}
	
	@Override
	public PageInfo listDataCount(PageInfo pageInfo){
		Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageInfo.getNowpage(), pageInfo.getPagesize());
		List<Map<String, Object>> list =  fundJournalMapper.listDataCount(page,pageInfo.getCondition());
		pageInfo.setRows(list);
		pageInfo.setTotal(page.getTotal());
		return pageInfo;
	}
	public List<Map<String, Object>> gridShowMonth(Map<String, Object> condition){
		return fundJournalMapper.gridShowMonth(condition);
	}
	public List<CountExportToExcel> countExportToExcel(Map<String, Object> condition){
		return fundJournalMapper.countExportToExcel(condition);
	}
	public List<PercentExportToExcel> percentExportToExcel(Map<String, Object> condition){
		return fundJournalMapper.percentExportToExcel(condition);
	}
	@Override
	public ResponseResult regressionLineShow() {
		RegressionLine record = null;
		int rows=regressionLineMapper.insert(record);
		RegressionLineUtils line = new RegressionLineUtils();
		if(rows>0){
			List<Map<String, Object>> list=regressionLineMapper.selectAll();
			  
			  for(int i=0;i<list.size();i++){
				  Float month=Float.parseFloat(list.get(i).get("month").toString());
				  Float money=Float.parseFloat(list.get(i).get("money").toString());
				  DataPoint dataPoint = new DataPoint(month, money);
				  line.addDataPoint(dataPoint);
			  }
			  printSums(line);  
			List<Float> list2= printLine(line);
			return  ResponseResult.createBySuccessData(list2);
		}else{
			return ResponseResult.createByFailedCode();
		}
		
		
	}
	
	 /** 
     * Print the computed sums. 
     *  
     * @param line 
     *            the regression line 
     */  
    private static void printSums(RegressionLineUtils line) {  
        System.out.println("\n数据点个数 n = " + line.getDataPointCount());  
        System.out.println("\nSum x  = " + line.getSumX());  
        System.out.println("Sum y  = " + line.getSumY());  
        System.out.println("Sum xx = " + line.getSumXX());  
        System.out.println("Sum xy = " + line.getSumXY());  
        System.out.println("Sum yy = " + line.getSumYY());  
  
    }  
  
    /** 
     * Print the regression line function. 
     *  
     * @param line 
     *            the regression line 
     */  
    private static List<Float> printLine(RegressionLineUtils line) {  
      System.out.println("\n回归线公式:  y = " + line.getA1() + "x + "  
              + line.getA0()); 
      System.out.println("误差：     R^2 = " + line.getR());  
      List<Float> list =new ArrayList<Float>();
      list.add(line.getA1());
      list.add(line.getA0());
      return list;
     
    }  
 
}
